package com.heitian.ssm.controller;

import com.github.pagehelper.PageHelper;
import com.heitian.ssm.dao.ItemDao;
import com.heitian.ssm.model.*;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Zhangxq on 2016/7/15.
 */

@Controller
@RequestMapping("/item")
public class ItemController {

    private Logger log = Logger.getLogger(UserController.class);
    @Resource
    private ItemDao itemDao;


    @RequestMapping(method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public JsonResponseee selectItem(@RequestParam("page") String page, @RequestParam("type") String type) {

        PageHelper.startPage(Integer.valueOf(page),5);
        List<Book> itemList = itemDao.getBook(Integer.valueOf(type));
        return new JsonResponseee(itemList);
    }

    @RequestMapping(value = "/collect",method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public JsonResponseee selectCollect(@RequestParam("page") String page, @RequestParam("uid") String uid) {

        PageHelper.startPage(Integer.valueOf(page),5);
        List<Book> itemList = itemDao.getCollect(Integer.valueOf(uid));
        return new JsonResponseee(itemList);
    }

    @RequestMapping(value = "/{bookid}", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public JsonResponseeee getBook(@PathVariable("bookid") String id,@RequestParam("page") String page) {

        Book book = itemDao.getOneBook(Integer.valueOf(id));
        PageHelper.startPage(Integer.valueOf(page),15);
        List<Section> itemList = itemDao.getSection(Integer.valueOf(id));
        return new JsonResponseeee(book, itemList);
    }

}
